package com.appcenter.message.controller;

import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import com.appcenter.message.input.SearchMessageListInput;
import com.appcenter.message.input.externalapi.ReadMsgInput;
import com.appcenter.message.input.externalapi.SendMsgInput;
import com.appcenter.message.service.ExternalAPISercive;
import com.appcenter.message.service.MQProducerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("services/api")
@Api(value="开放API接口",description = "开放API接口")
public class ExternalAPIController {

    @Autowired
    ExternalAPISercive externalAPISercive;
    @Autowired
    MQProducerService mqService;



    @ApiOperation(value = "发送消息", notes = "发送消息",produces = "application/json")
    @PostMapping("/sendmsg")
    public ResponseEntity<JsonResult> SendMsg(@RequestBody SendMsgInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setSuccess(true);
            r.setResult(externalAPISercive.SendMsg(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "异步发送消息", notes = "异步发送消息",produces = "application/json")
    @PostMapping("/aysnsendmsg")
    public ResponseEntity<JsonResult> AysnSendMsg(@RequestBody SendMsgInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setSuccess(true);
            mqService.sendMessage(input);
            r.setResult("成功");
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "读取消息", notes = "读取消息",produces = "application/json")
    @PostMapping("/readmsg")
    public ResponseEntity<JsonResult> readMessage(@RequestBody ReadMsgInput input) throws Exception {

        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPISercive.ReadMsg(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);

    }

    @ApiOperation(value = "查询消息信息", notes = "查询消息信息",produces = "application/json")
    @PostMapping("/getmsgbyusers")
    public ResponseEntity<JsonResult> GetMsgByUsers(@RequestBody SearchMessageListInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPISercive.GetMsgByUsers(input));

        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);


    }
}
